package com.ldk.springboot2021.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ldk.springboot2021.mapper.DoorMapper;
import com.ldk.springboot2021.pojo.Door;
import com.ldk.springboot2021.service.DoorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.Date;
import java.util.List;

@Service//这个类加了这个注解，才能被Spring管理
public class DoorServiceImpl implements DoorService {
    //注入持久层对象

@Autowired(required = false)
private DoorMapper doorMapper;

    @Override
    public String addDoorInfo(String doorName, String tel, Model model) {
        if(doorName.equals("")||doorName==null){
            model.addAttribute("msg","门店名必填");
            return "addDoor";
        }

        if(tel.equals("")||tel==null){
            model.addAttribute("msg","电话必填");
            return "addDoor";
        }
        //封装参数
        Door door=new Door().setDoorName(doorName).setTel(tel).setCreateTime(new Date());
        int row = doorMapper.insert(door);
        //对受影响行数进行判断
        if(row!=1){
            //将错误信息提示到页面addDoor.html
            model.addAttribute("msg","添加失败");
            return "addDoor";
        }
        //查询门店信息，绑定到doorList页面
        return "redirect:/door/findAllDoors";//重定向就是找别的请求干活

    }

    @Override
    public String findAllDoorsInfo(Model model) {
        QueryWrapper<Door> queryWrapper=new QueryWrapper<>();
        List<Door> list = doorMapper.selectList(queryWrapper);
        model.addAttribute("doorList",list);
        return "doorList";//跳转到门店列表

    }

    @Override
    public String deleteByDoorId(Integer doorId) {
        doorMapper.deleteById(doorId);
        return "redirect:/door/findAllDoors";
    }

    @Override
    public String toUpdateByDoorId(Integer doorId, Model model) {
//        System.out.println("doorId::"+doorId);
        Door door = doorMapper.selectById(doorId);
//        System.out.println(door);
        model.addAttribute("door",door);
        return "updateDoor";
    }

    @Override
    public String updateById(Door door) {
        door.setModifyTime(new Date());
        doorMapper.updateById(door);
        return "redirect:/door/findAllDoors";
    }

}
